package kr.hkit.powerjava.chapter24.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.hkit.powerjava.chapter24.work.Student;

public class StudentDao implements InterfaceDao<Student>{
	private ArrayList<Student> lists;
	private static final String FILE_NAME = "studentList.data";
	
	@Override
	public void insertItem(Student item) {
		if(!lists.contains(item)){
			lists.add(item);
		}else
			System.out.println("중복된 데이터");
	}

	@Override
	public void deleteItem(Student item) {
		lists.remove(item);
	}

	@Override
	public void updateItem(Student item) {
		lists.set(lists.indexOf(item), item);
	}
	

	@Override
	public Student searchItem(Student item) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void prnItemList(Comparator<Student> comp) {
		if(comp == null){
			Collections.sort(lists);
		}else{
			Collections.sort(lists, comp);
		}
		for(Student std : lists){
			System.out.println(std);
		}
	}

	@Override
	public void loadFile() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(FILE_NAME)));
			lists = (ArrayList<Student>) ois.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void saveFile() {
		// TODO Auto-generated method stub
		
	}

}
